package com.sicnu.cheer.leaugebar.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.sicnu.cheer.leaugebar.R;
import com.sicnu.cheer.leaugebar.bean.ShareBean;

import java.util.List;

/**
 * 分享adapter
 * Created by cheer on 2017/3/14.
 */

public class ShareAdapter extends RecyclerView.Adapter<ShareAdapter.ViewHolder> {
    private List<ShareBean> data;
    private OnItemClickListener onItemClickListener;
    private ShareAdapter mThis;

    public ShareAdapter(List<ShareBean> data) {
        mThis = this;
        this.data = data;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_share_item, null);
        ViewHolder holder = new ViewHolder(view);
        view.setTag(holder);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v.getTag() != null) {
                    ViewHolder holder = (ViewHolder) v.getTag();
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(mThis, holder.position);
                    }
                }
            }
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.position = position;
        ShareBean item = data.get(position);
        holder.imageView.setImageResource(item.getImageId());
        holder.nameView.setText(item.getName());
    }


    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        }
        return 0;
    }




    class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView nameView;
        private int position;

        public ViewHolder(View itemView) {
            super(itemView);
            imageView = (ImageView) itemView.findViewById(R.id.share_image);
            nameView = (TextView) itemView.findViewById(R.id.share_name);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(RecyclerView.Adapter adapter, int position);
    }
}
